/**
 * Created by lipeng on 16/12/7.
 */

/**
 * 注册方法
 * @param req
 * @param res
 */

var commmon = require("../models/common");
var db = commmon.createNew();

var register = function (req,res) {
    var sql = "select * from user";
    db.query(sql,[],function (err,result) {
        res.render('user/register',{datas:result});
    });
};

var doRegister = function (req,res) {

    var username = req.body.username;
    var password = req.body.password;
    console.log(username);
    var sql1 = "select * from user where `username` = ?";
    db.query(sql1,[username],function (err,find_res) {
        if(err){
            res.json({"error":20000,"info":"数据库错误!"});
        }else{
            if((typeof find_res == "object") && find_res.length){
                res.json({"error":20002,"info":"改用户已经存在!"});
            }else{
                var sql2 = "insert into user(`username`,`password`) values(?,?)";
                db.query(sql2,[username,password],function (err,result) {
                    if(err){
                        res.json({"error":20001,"info":"注册失败!"});
                    }else{
                        res.json({"error":200,"info":"注册成功!"});
                    }
                });
            }
        }
    });
};
/**
 * 登陆方法
 * @param req
 * @param res
 */
var login = function (req,res) {
    res.render('user/login');
};

var doLogin = function (req,res) {

};

module.exports = {
    'register':register,
    'login':login,
    'doRegister':doRegister,
    'doLogin':doLogin
};